/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legsem.legstar.cci;

import com.legstar.host.access.HostAccessStrategy;
import com.legstar.host.invoke.CommareaInvoker;
import com.legstar.host.invoke.ContainerInvoker;
import com.legstar.host.invoke.HostInvoker;
import com.legstar.host.invoke.HostInvokerException;
import com.legstar.host.invoke.model.HostProgram;
import com.legstar.messaging.LegStarAddress;

/**
 * Create an invoker depending on target program.
 * 
 */
public final class LegStarManagedHostInvokerFactory {

    /** Utility class. */
    private LegStarManagedHostInvokerFactory() {

    }

    /**
     * An Invoker is constructed from a configuration, for a particular host
     * address and target host program.
     * 
     * @param address the host address
     * @param accessStrategy the host access strategy (direct, pooled or managed
     *            ...)
     * @param hostProgram the host program bean file
     * @return a Host invoke implementation
     * @throws HostInvokerException in construction fails
     */
    public static HostInvoker createHostInvoker(final LegStarAddress address,
            final HostAccessStrategy accessStrategy,
            final HostProgram hostProgram) throws HostInvokerException {

        /* If a Channel is specified, this is a request for LINK CHANNEL */
        String channelName = hostProgram.getChannelName();
        if (channelName != null && channelName.length() > 0) {
            return new ContainerInvoker(accessStrategy, address, hostProgram);
        }

        /* The default is a commarea invoke */
        return new CommareaInvoker(accessStrategy, address, hostProgram);
    }

}
